package com.witdata.rabbit.display;

import com.alibaba.fastjson2.JSON;
import com.witdata.common.domain.docker.Execute;
import com.witdata.mqtt.service.MqttMessageSender;
import com.witdata.rabbit.producer.InstanceProducer;
import com.witdata.rabbit.service.InstanceService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

@Data
@RequiredArgsConstructor
@Component
@Slf4j
public class ExecuteDisplay {
    private final InstanceService instanceService;
    private final InstanceProducer instanceProducer;
    private final MqttMessageSender mqttMessageSender;

    public void sendExecuteResponse(String data) {
        Execute execute = JSON.parseObject(data, Execute.class);
        if (!ObjectUtils.isEmpty(execute) && StringUtils.hasText(execute.getId())){
            mqttMessageSender.send("/execute/message/"+execute.getId(), execute.getOutput());
        }
    }

}
